﻿#include "common.h"
#include "VolumeControl.h"
#include <QVBoxLayout>

VolumeControl::VolumeControl(QWidget* parent)
    : QWidget(parent), volumeSlider(new QSlider(Qt::Horizontal, this)), volumeLabel(new QLabel(this)), volumeDisplayLabel(new QLabel(this))
{
    volumeSlider->setRange(0, 100);
    volumeSlider->setValue(50);
    volumeSlider->setStyleSheet("QSlider::groove:horizontal { height: 8px; background: #c4c4c4; } "
        "QSlider::handle:horizontal { width: 16px; height: 16px; background: #666666; border-radius: 8px; } "
        "QSlider::add-page:horizontal { background: #666666; } "
        "QSlider::sub-page:horizontal { background: #666666; }");

    volumeLabel->setText("Volume:");
    volumeDisplayLabel->setText(QString::number(volumeSlider->value()) + "%");

    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addWidget(volumeLabel);
    layout->addWidget(volumeSlider);
    layout->addWidget(volumeDisplayLabel);

    setLayout(layout);

    connect(volumeSlider, &QSlider::valueChanged, this, &VolumeControl::setVolume);
}

void VolumeControl::enterEvent(QEnterEvent* event)
{
    QWidget::enterEvent(event);
    volumeSlider->show();
}

void VolumeControl::leaveEvent(QEvent* event)
{
    QWidget::leaveEvent(event);
    volumeSlider->hide();
}

bool VolumeControl::event(QEvent* event)
{
    if (event->type() == QEvent::ToolTip) {
        QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);
        QToolTip::showText(helpEvent->globalPos(), QString::number(volumeSlider->value()) + "%", this);
        return true;
    }
    return QWidget::event(event);
}

void VolumeControl::setVolume(int value)
{
    volumeDisplayLabel->setText(QString::number(value) + "%");
}

//#include "moc_MyWindow.cpp"